﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using S.Common.ApiResult;
using S.Common.helper;
using S.Weaning.Domain.Dto;
using static S.Weaning.Read.ApI.Application.Command.LambAblactationCommand;
using static S.Weaning.Read.ApI.Application.Command.MaternalWeaningCommand;

namespace S.Weaning.Read.ApI.Controllers
{
    /// <summary>
    /// 羔羊断奶读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LambAblactationController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IConfiguration configuration;

        public LambAblactationController(IMediator mediator, IConfiguration configuration)
        { 
            this.mediator = mediator;
            this.configuration = configuration;
        }
        /// <summary>
        /// 羔羊断奶显示
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<ApiResult<List<LambAblactationDto>>> ShowLambAblactation(string? ddate, string? ear, int dayage, decimal? weightmin, decimal? weightgmax, string? maternal,int page,int size)
        {
            var list = await mediator.Send(new ShowLambAblactationCommand(ddate, ear, dayage, weightmin, weightgmax, maternal,page, size));
            list.StatusCode = 200;
            list.Message = "success";
            list.Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            return list;
        }
        /// <summary>
        /// 断奶羔羊信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> IndexLambablactation()
        {
            var list = await mediator.Send(new IndexLambAblactationCommand());
            return Ok(list);
        }
        /// <summary>
        /// 反填羔羊
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetFieldModel(string ear)
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway+ "/ParturitionRead/api/Parturition/GetLambList");
            var response = JsonConvert.DeserializeObject<List<LambListDto>>(jsonString).Where(t=>t.EarTag==ear);
            return Ok(response);
        }
        /// <summary>
        /// 断奶羔羊信息反填（修改）
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> LambablactationById(int id)
        {
            var list = await mediator.Send(new LambAblactationByIdCommand(id));
            return Ok(list);
        }
    }
}
